/**
 * 
 */
package com.yidu.view;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yidu.bean.TbImage;
import com.yidu.bean.TbManager;
import com.yidu.bean.TbManagerRole;
import com.yidu.bean.TbMember;
import com.yidu.bean.TbShop;
import com.yidu.commom.Tool;
import com.yidu.message.Message;
import com.yidu.message.Pages;
import com.yidu.service.TbImageService;
import com.yidu.service.TbManagerRoleService;
import com.yidu.service.TbManagerService;

/**
 *
 * @author yichengming
 * 2018年7月17日上午10:34:46
 * @version 1.0
 */
@Controller
@RequestMapping("/manager")
public class TbManagerAction {

	Logger logger = Logger.getLogger(TbManagerAction.class);
	//消息信息类
	Message msg = new Message();
	@Resource
	private TbManagerService manService;
	@Resource
	private TbImageService imageService;

	@Resource
	private TbManagerRoleService  managerRoleService;


	//json	
	ObjectMapper mapper = new ObjectMapper();

	
	
	
	@RequestMapping("/add")
	@ResponseBody
	public Message add(HttpSession session){
		
		TbShop shop = (TbShop)session.getAttribute("showShop");
		
		TbManager tbManager = new TbManager();
		//tbManager.setManUser("345678");
		tbManager.setManId(Tool.getRandomString(32));
		tbManager.setShopId(shop.getShopId());
		tbManager.setManPwd("123456");
		tbManager.setManUser(shop.getLinkman());
		
		
		
		 
		int man = manService.save(tbManager);
		msg.setNum(man);
		if(man>0) { 
			msg.setMsg("增加成功");
			
			try{
				//默认增加店长
				TbManagerRole mr = new TbManagerRole();
				mr.setMrId(Tool.getRandomString(32));
				mr.setManId(tbManager.getManId());
				mr.setRoleId("kk0jtctqi4yegzkqcqmjjf69bv7o6btx");
				managerRoleService.save(mr);
			}catch (Exception e) {
				e.getMessage();
				e.getStackTrace();
			}
			 
			
		}else { 
			msg.setMsg("增加失败"); 
		}
		return msg;
	}

	
	
	/**
	 *  接受前台 传递过来的参数<br>
	 *  执行登录判断
	 * @param memId
	 * @return
	 */
	@RequestMapping("/isLogin")
	@ResponseBody
	public  Message isLogin(TbManager man,HttpSession session){

		logger.info("进入根据查询名字..2222222222..");
		int rows= manService.isLogin(man);

		TbManager mans = manService.findBy(man);

		session.setAttribute("manager", mans);
		if(rows>0){
			msg.setMsg("1");
			return msg;
		}else{ 
			msg.setMsg("0");
			return msg;
		} 
	}




	/**
	 * 进入后台登录界面
	 * @return
	 */
	@RequestMapping("/showLogin")
	private String showLogin(){
		logger.info("进入后台登录界面...");
		return "BackgroundLog";
	}

	/**
	 * 根据ID查询对象类型
	 * 
	 * @param id
	 * @param response
	 * @return
	 */
	@RequestMapping("/showUpdate")
	@ResponseBody
	public Object showUpdate(String manId,HttpSession session){
		logger.info("进入根据ID查询...");
		TbManager manager = manService.findById(manId);
		
		session.setAttribute("showManager", manager);
		
		return manager;


	}

	/**
	 * 自行判断 增加 修改<br>
	 * 根据ID是否存在
	 * @param member
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value="/saveOrUpdate",method=RequestMethod.POST)
	@ResponseBody
	public Object saveOrUpdate(HttpServletRequest request,TbManager manager,String str) throws IOException {

		logger.info("进入增加修改方法...");

		logger.info(mapper.writeValueAsString(manager));

		
		 
		//判断是否存在id 
		int num = 0;


		if(!Tool.isEmpty(manager.getManId())){
			//logger.info("");
			//存在 执行修改 
			num = manService.update(manager);

			//	num = imageService.update(image);


			//分配角色
			//首先删除
			managerRoleService.deleteMan(manager.getManId());
 


		}else{
			//反之 执行增加
			//自行增加主键
			manager.setManId(Tool.getRandomString(32));
			num  =manService.save(manager);

		}

		//增加
		String[] attr = str.split(",");
		for (int i = 0; i < attr.length; i++) {
			TbManagerRole mr = new TbManagerRole();
			mr.setMrId(Tool.getRandomString(32));
			mr.setManId(manager.getManId());
			mr.setRoleId(attr[i]);
			num = managerRoleService.save(mr); 
		}

 





		//得到业务ID
		TbImage img = imageService.findById(manager.getManId()); 

		logger.info(mapper.writeValueAsString(img));


		//判断是否为空
		if(!StringUtils.isEmpty(img)){
			//修改图片 
			img.setIamge(manager.getFileName());
			img.setSort(1);
			num = imageService.update(img);
		}else{
			//增加 图片
			TbImage image = new TbImage();
			image.setYewuId(manager.getManId());
			image.setImgId(Tool.getRandomString(32));
			image.setIamge(manager.getFileName());
			image.setSort(1);
			num = imageService.save(image);
		} 
		//根据 操作返回
		msg.setNum(num);
		if(num>0){
			msg.setMsg("操作成功!");
		}else{
			msg.setMsg("操作失败!");
		}
		return msg; 
	}

	/**
	 *  接受前台 传递过来的参数<br>
	 *  执行删除方法
	 * @param memId
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public  Object delete(String manId){


		logger.info("进入删除方法....");
		int row = manService.delete(manId);
		//HttpServletResponse response = ServletActionContext.getResponse();
		//response.setCharacterEncoding("utf-8");
		if(row>0){
			msg.setMsg("删除成功!");
		}else{ 
			msg.setMsg("删除失败!");
		} 
		return msg;
	}

	@RequestMapping("/findVague")
	@ResponseBody	
	public Object findVague(Pages pages,TbManager manager) throws JsonProcessingException{
		logger.info("进入模糊查询...");
		logger.info(mapper.writeValueAsString(manager));



		List<TbManager> list = manService.findVague(pages, manager);

		for (int i = 0; i < list.size(); i++) {
			TbManager managers = list.get(i);

			//得到业务ID 查询出图片
			TbImage img = imageService.findById(managers.getManId());

			//判断是否为空  
			if(!StringUtils.isEmpty(img)){ 
				managers.setFileName("../../../"+img.getIamge());
			}


			list.set(i, managers);

		} 

		int row = manService.findByCount(manager);



		//由于前端所需条件
		Map<String, Object> map = new HashMap<>();

		map.put("code", 0);
		map.put("msg","");
		map.put("count",row);
		map.put("data", list);
		return map; 

	}



	/**
	 * 登录
	 * @param session
	 * @param man
	 * @return
	 *//*
	@RequestMapping("/findByName")
	private String findByName(HttpSession session,TbManager man){
		logger.info("进入后台登录判断...");

		TbManager manager = managerService.findByName(man);

		//将用户存储在session中
		session.setAttribute("manager",manager);

		return "background/index";

	}*/
}
